//  TABLE 2

use "data/table2/table2.dta", clear

// main variables used in the analysis
des real_ada dem maj dgap dwgap cst_n

eststo clear

// Declare outcome variable: ada score (average in term)
global y real_ada

//Decleare kernel
global kernel uniform

//Declare running variable
replace dgap=100*dgap
replace dwgap=100*dwgap
global run dgap
global run_reg dgap dwgap

// Optimal CCT bandwidth
rdbwselect $y $run , kernel($kernel)
g bw = 1 if $run >- e(h_mserd) & $run <e(h_mserd)
scalar h=e(h_mserd)

// For standard errors clustered 
global cluster cst_n  // district level

// Year fixed effect
quietly: tab yr, g(dyr)


**********************************
* SAMPLE FULL 1946-2006 (sample6)
**********************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample6==1 , cluster($cluster) // D
		estadd scalar obs = e(N) 
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h

		
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample6==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		
		
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample6==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 		
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])			
		estadd scalar bw = h

		
		
********************************
*  SAMPLE DEM 1978-1992 (sample4)
********************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample4==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100
		estadd scalar bw = h

		
		
		eststo: reg $y dem  dyr* $run_reg if bw==1 & sample4==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100
		estadd scalar bw = h		

		
		
********************************
* SAMPLE REP 1994-2004 (sample5)
********************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample5==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 0
		estadd scalar bw = h

				
		
		eststo: reg $y dem  dyr* $run_reg if bw==1 & sample5==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 0		
		estadd scalar bw = h

		
		
*****************************************
*  SAMPLE DEM-SKEWED 1978-1994 (sample1)
*****************************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample1==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h

		
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample1==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)	
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample1==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])	
		estadd scalar bw = h		

		
		
****************************************
* SAMPLE REP-SKEWED 1992-2004 (sample2)
****************************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample2==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h

		
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample2==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample2==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])			
		estadd scalar bw = h	

		
		
*****************************************************************
// samples 6 4 5 1 2
	** View table
			#delimit ;
			esttab ,
				 se(2) b(2) nonotes 
				keep(dem maj)
				stats(
					timefe ydem yrep ndemyr corr obs,
					labels(
						"Electoral cycle FE"
						"Mean Y if D=1"
						"Mean Y if D=0"
						"Obs. in dem years (%)"
						"Corr(D,M)"
						"Observations"
						)
					fmt(%3s 0 0 0 2 0)
				)
				star(* 0.10 ** 0.05 *** 0.01)
				replace ;
			#delimit cr	
	

	
	** Latex
			#delimit ;
			esttab  using "results/table2_upper.tex",
				booktabs se(2) b(2) nonotes  nomtitles nostar
				keep(dem maj)
				stats(
					timefe ydem yrep ndemyr corr obs,
					labels(
						"Electoral cycle FE"
						"Mean Y if D=1"
						"Mean Y if D=0"
						"Obs. in dem years (\%)"
						"Corr(D,M)"
						"Observations"
						)
					fmt(%3s 0 0 0 2 0)
				)
				mgroups(
					"1946-2006" "Dem. control: 1978-1992" "Rep. control: 1994-2004"  "1978-1994" "1990-2004"
					, pattern(1 0 0 1 0 1 0  1 0 0 1 0 0) 
					prefix(\multicolumn{@span}{c}{) 
					suffix(}) 
					span 
					erepeat(\cmidrule(lr){@span})
				)
				varlabels(
					dem "D"
					maj "M"
					)
				replace ;
			#delimit cr
		
	
eststo clear
		
///////////////////////////////////////////////////////////////////
// SECOND BLOCK
/////////////////////////////////////////////////////////////////
			
			
******************************
*  SAMPLE 1978-2004 (sample3)
******************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample3==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample3==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample3==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		

				

*********************************
*  SAMPLE DEM 1954-1976 (sample8)
*********************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample8==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem  dyr* $run_reg if bw==1 & sample8==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100
		estadd scalar bw = h		

			

		
		
**********************************
* SAMPLE REP 1946 & 1952 (sample7)
**********************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample7==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 0
		estadd scalar bw = h

				

		
		
		eststo: reg $y dem  dyr* $run_reg if bw==1 & sample7==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 0		
		estadd scalar bw = h

		

		
		
****************************************
* SAMPLE DEM-SKEWED 1946-1976 (sample10)
****************************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample10==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample10==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample10==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])			
		estadd scalar bw = h

		

		
		
***************************************
*  SAMPLE BALANCED? 1946-1952 (sample9)
***************************************
	 
		eststo: reg $y dem $run_reg if bw==1 & sample9==1 , cluster($cluster) // D
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem maj $run_reg if bw==1 & sample9==1 , cluster($cluster) // D M
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "No" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)	
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])		
		estadd scalar bw = h

		

		
		
		eststo: reg $y dem maj dyr* $run_reg if bw==1 & sample9==1, cluster($cluster) // D M FE
		estadd scalar obs = e(N)
		corr dem maj if e(sample)
		estadd scalar corr = r(rho) 
		estadd local timefe "Yes" 
		quietly: sum $y if e(sample) & dem==1
		estadd scalar ydem = r(mean)
		quietly: sum $y if e(sample) & dem==0
		estadd scalar yrep = r(mean)
		estadd scalar d_pchange = 100*(_b[dem])/r(mean)
		quietly: tab demmaj if e(sample), matcell(x)
		estadd scalar ndemyr = 100* x[2,1]/(x[2,1]+x[1,1])	
		estadd scalar bw = h

		

		
		
		

	
*****************************************************************
// samples 3 8 7 10 9
	** View table
			#delimit ;
			esttab ,
				 se(2) b(2) nonotes 
				keep(dem maj)
				stats(
					timefe ydem yrep ndemyr corr obs,
					labels(
						"Electoral cycle FE"
						"Mean Y if D=1"
						"Mean Y if D=0"
						"Obs. in dem years (%)"
						"Corr(D,M)"
						"Observations"
						)
					fmt(%3s 0 0 0 2 0)
				)
				star(* 0.10 ** 0.05 *** 0.01)
				replace ;
			#delimit cr	
	
	
	** Latex
			#delimit ;
			esttab  using "results/table2_lower.tex",
				booktabs se(2) b(2) nonotes  nomtitles nostar
				keep(dem maj)
				stats(
					timefe ydem yrep ndemyr corr obs,
					labels(
						"Electoral cycle FE"
						"Mean Y if D=1"
						"Mean Y if D=0"
						"Obs. in dem years (\%) "
						"Corr(D,M)"
						"Observations"
						)
					fmt(%3s 0 0 0 2 0)
				)
				mgroups(
					"1982-2004" "Dem. control: 1954-1976" "Rep. control: 1946+1952" " 1946-1976" "1946-1958"
					, pattern(1 0 0 1 0 1 0  1 0 0 1 0 0) 
					prefix(\multicolumn{@span}{c}{) 
					suffix(}) 
					span 
					erepeat(\cmidrule(lr){@span})
				)
				varlabels(
					dem "D"
					maj "M"
					)
				replace ;
			#delimit cr

			
eststo clear
exit
